// component/ui/z-city/z-city.js
import * as transferData from '../../../models/transferData'
Component({
  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/
  },
  /**
   * 组件的属性列表
   */
  properties: {
    mode: {
      type: String,
      value: 'jump',
    },
    cityName: {
      type: String,
      value: '',
    },
    border: {
      type: Boolean,
      value: false,
    },
    local: {
      type: Boolean,
      value: false,
    },
    name: {
      type: String,
      value: '',
    },
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 页面的生命周期
   */
  pageLifetimes: {
    show: function () {
      this.updateCity()
    },
    hide: function () {},
    resize: function (size) {}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onClick() {
      const {
        mode,
        name,
        local
      } = this.data
      switch (mode) {
        case "jump":
          wx.navigateTo({
            url: `/pages/home/switchCity/switchCity?local=${local ? '1' : ''}&name=${name ? name : ''}`,
          })
          break;
        case "popup":
          this.triggerEvent("open")
          break;
      }
    },

    updateCity() {
      const {
        name
      } = this.data
      let city = transferData.getTemporaryData(name)
      if (city) {
        transferData.resetTemporaryData(name)
        this.triggerEvent('change', city)
      }
      let city1 = transferData.getData('currentCity')
      if (city1) {
        transferData.resetData('currentCity')
        this.triggerEvent('change', city1)
      }
    },
  }
})